service.service('BaseService',['$q',function($q){
      var service = {};
      var Order = AV.Object.extend("Order");
      var WarHouse = AV.Object.extend("WarHouse");
      service.barscanHandle= function (uid,oid,myLong,myLat) {
        var deferred = $q.defer();
        async.auto({
          statusOfScan:function(callback){
            var orderQuery = new AV.Query(Order);
            orderQuery.equalTo('orderid',oid);
            orderQuery.first().then(function(res){
              if (res){
                var shopId = res.get('shopId');
                var shopInfo = new AV.Query(WarHouse);
                shopInfo.get(shopId).then(function(s){
                  var lng = s.get('lng'),
                      lat = s.get('lat');
                  var distance = coordtranform.distance({long:myLong,lat:myLat},{long:lng,lat:lat});
                  if(distance > s.get('distance')){
                      return callback(null, {code:12,msg:'已超出可扫码范围！请离仓库近一点！！',distance:distance})
                  }else{
                    //return callback(null, {code:12,msg:'已超出可扫码范围！请离仓库近一点！！',distance:distance})
                    var status = res.get('status');
                    var messioned = res.get('messioned');
                    var messionedUser = res.get('messionedUser');
                    if (status == 4000 || status == 7000){
                      if (messioned === true){
                        if (messionedUser == uid){
                          return callback(null,{code:3,msg:'自己已领订单',distance:distance});
                        }else{
                          return callback(null,{code:4,msg:'订单已被领，转接订单',distance:distance});
                        }
                      }else{
                        return callback(null,{code:2,msg:'订单，未被领，可以扫',distance:distance});
                      }
                    }else if(status == -1){
                      return callback(null,{code:8,msg:'用户已撤销订单',distance:distance});
                    }else if(status == -2){
                      return callback(null,{code:9,msg:'平台已撤销订单',distance:distance});
                    }else if(status == -3){
                      return callback(null,{code:10,msg:'订单已失效',distance:distance});
                    }else if(status == 2500 || status == 3000){
                      return callback(null,{code:11,msg:'订单未打包结束、不能领取',distance:distance});
                    }else {
                      return callback(null,{code:5,msg:'订单已被处理',distance:distance});
                    }
                  }
                })
              }else{
                return callback(null,{code:7,msg:'系统没有此订单',distance:distance});
              }
            }).catch(function(err){
              console.log(err);
              callback(err);
            });
          },
          startScan:["statusOfScan",function(callback,results){
            var status = results.statusOfScan.code ;
            var list = {case:0,info:{},distance:0} ;
            switch(status){
              case 2 :
                list['case'] = 2 ;
                list['info']['updateOrder'] = 1;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              case 3 :
                list['case'] = 3 ;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              case 4 :
                list['case'] = 4 ;
                list['info']['updateOrder'] = 1;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              case 5 :
                list['case'] = 5 ;
                list['info']['over'] = 1;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              case 7 :
                list['case'] = 7 ;
                list['info']['over'] = 1;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              case 8 :
                list['case'] = 8 ;
                list['info']['over'] = 1;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              case 9 :
                list['case'] = 9 ;
                list['info']['over'] = 1;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              case 10 :
                list['case'] = 10 ;
                list['info']['over'] = 1;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              case 11 :
                list['case'] = 11 ;
                list['info']['over'] = 1;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              case 12 :
                list['case'] = 12 ;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
              default :
                list['case'] = 6 ;
                list['info']['over'] = 1;
                list['distance'] = results.statusOfScan.distance;
                callback(null,list);
                break;
            }
          }],
          updaetOrder:["startScan",function(callback,results){
            var status = results.startScan.case ;
            switch(status){
              case 2 :
                var orderQuery = new AV.Query(Order);
                orderQuery.equalTo('orderid',oid);
                orderQuery.first().then(function(res){
                  var syncs = res.get('syncStatus');
                  if(syncs == 0){
                    res.set('syncStatus',2);
                  }
                  res.set('messioned',true);
                  res.set('messionedUser',uid);
                  res.set('status',7000);
                  date = new Date();
                  var time = Date.parse(date);
                  res.set('scantime',time);
                  res.set('startTime',new Date());
                  res.save().then(function(){
                    callback(null,{code:1,msg:'领单成功'+results.startScan.distance});
                  });
                });
                break;
              case 3 :
                callback(null,{code:2,msg:'你已领取此单，不可重复领取'+results.startScan.distance});
                break;
              case 4 :
                var r=confirm("该订单已被领单，确认转接订单吗!");
                if (r==true) {
                  var orderQuery = new AV.Query(Order);
                  orderQuery.equalTo('orderid',oid);
                  orderQuery.first().then(function(res){
                    var syncs = res.get('syncStatus');
                    if(syncs == 0){
                      res.set('syncStatus',2);
                    }
                    res.set('messioned',true);
                    res.set('messionedUser',uid);
                    res.set('status',7000);
                    date = new Date();
                    var time = Date.parse(date);
                    res.set('scantime',time);
                    res.set('startTime',new Date());
                    res.save().then(function(){
                      callback(null,{code:3,msg:'转接订单成功'+results.startScan.distance});
                    });
                  });
                }else{
                  callback(null,{code:2,msg:'取消转接订单!'+results.startScan.distance});
                }
                break;
              case 5 :
                callback(null,{code:1,msg:"订单已被处理"+results.startScan.distance});
                break;
              case 7 :
                callback(null,{code:1,msg:"系统没有此订单"+results.startScan.distance});
                break;
              case 8 :
                callback(null,{code:1,msg:"用户已撤销订单"+results.startScan.distance});
                break;
              case 9 :
                callback(null,{code:1,msg:"平台已撤销订单"+results.startScan.distance});
                break;
              case 10 :
                callback(null,{code:10,msg:"订单已失效"+results.startScan.distance});
                break;
              case 11 :
                callback(null,{code:11,msg:"订单未打包结束、不能领取"+results.startScan.distance});
                break;
              case 12 :
                callback(null,{code:12,msg:"已超出可扫码范围！请离仓库近一点！！当前距离"+results.startScan.distance+"米"});
                break;
              default :
                callback(null,{code:0,msg:"出错了"});
                break;
            }
          }],
          finalFun:["updaetOrder",function(callback,results){
            callback(null,{code:0,msg:results.updaetOrder.msg});
          }]
        },function(err,results){
          if(err){
            deferred.reject(err);
          }else{
            deferred.resolve(results.finalFun);
          }
        });
        return deferred.promise;
      };
      return service;
    }]);

app.factory('Tasks', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var data = [{
    id: 0,
    name: 'Ben Sparrow',
    lastText: 'You on your way?',
    face: 'img/ben.png'
  }, {
    id: 1,
    name: 'Max Lynx',
    lastText: 'Hey, it\'s me',
    face: 'img/max.png'
  }, {
    id: 2,
    name: 'Adam Bradleyson',
    lastText: 'I should buy a boat',
    face: 'img/adam.jpg'
  }, {
    id: 3,
    name: 'Perry Governor',
    lastText: 'Look at my mukluks!',
    face: 'img/perry.png'
  }, {
    id: 4,
    name: 'Mike Harrington',
    lastText: 'This is wicked good ice cream.',
    face: 'img/mike.png'
  }];

  return {
    all: function() {
      return data;
    },
    remove: function(task) {
      data.splice(data.indexOf(task), 1);
    },
    get: function(taskId) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].id === parseInt(taskId)) {
          return data[i];
        }
      }
      return null;
    }
  };
});
